package io.zcoder.innoway.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;
import io.zcoder.innoway.R;
import io.zcoder.innoway.utils.Utils;
import io.zcoder.innoway.commons.PrefUtils;
import io.zcoder.innoway.models.Profile;
import io.zcoder.innoway.widgets.TextViewCustom;

public class ProfileAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater mInflater;
    private List<Profile> mProfiles;
    private ProfileCallBack mCallBack;
    private int mProfileId;
    private int mProfileMainId;

    public ProfileAdapter(Context context, List<Profile> profiles) {
        this.mContext = context;
        this.mProfiles = profiles;
        Profile profile = new Profile();
        profile.setFirst_name(mContext.getString(R.string.guest));
        profile.setLast_name("");
        mProfiles.add(profile);
        this.mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mProfileId = PrefUtils.getInt(mContext, PrefUtils.PREF_PROFILE_ID);
        if (mContext instanceof ProfileCallBack) {
            mCallBack = (ProfileCallBack) mContext;
        }
        for (Profile profile1 : mProfiles) {
            if (profile1.getRelation() == 0) {
                mProfileMainId = profile1.getId();
                break;
            }
        }
    }

    @Override
    public int getCount() {
        return mProfiles != null ? mProfiles.size() : 0;
    }

    @Override
    public Profile getItem(int position) {
        return mProfiles.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public int getProfileMainId() {
        return mProfileMainId;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_family, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final Profile item = getItem(position);
        if (!Utils.isBlank(item.getAvatar_url())) {
            Picasso.with(mContext).load(item.getAvatar_url()).into(holder.avatar);
        } else {
            holder.avatar.setImageResource(0);
        }
        if (item.getId() == mProfileId) {
            holder.check.setVisibility(View.VISIBLE);
            holder.time.setText(mContext.getString(R.string.my_profile));
            holder.time.setVisibility(View.VISIBLE);
        } else {
            holder.check.setVisibility(View.GONE);
            if (!Utils.isBlank(item.getLast_used_time())) {
                holder.time.setVisibility(View.VISIBLE);
                holder.time.setText(Utils.convertToString(Utils.convertToDate(item.getLast_used_time(),
                        Utils.DATE_FORMAT_FULL), Utils.DATE_FORMAT_MMM_DD_YYYY));
            } else {
                holder.time.setVisibility(View.GONE);
            }
        }
        holder.name.setText(item.getFirst_name() + " " + item.getLast_name());
        holder.layoutItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCallBack != null) {
                    if (position == getCount() - 1) {
                        return;
                    }
                    if (mProfileId != item.getId()) {
                        mCallBack.onSelectProfile(item);
                    }
                }
            }
        });
        if (position == getCount() - 1) {
            holder.avatar.setImageResource(0);
            holder.dot.setVisibility(View.GONE);
        } else {
            holder.dot.setVisibility(View.VISIBLE);
        }
        holder.dot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCallBack != null) {
                    if (item.getId() == mProfileId) {
                        mCallBack.optionProfile(item);
                        return;
                    }
                    if (mProfileId == mProfileMainId) {
                        mCallBack.optionProfileFromMain(item);
                        return;
                    }

                }
            }
        });

        return convertView;
    }

    public class ViewHolder {
        @BindView(R.id.avatar)
        CircleImageView avatar;
        @BindView(R.id.ic_check)
        ImageView check;
        @BindView(R.id.name)
        TextViewCustom name;
        @BindView(R.id.time)
        TextViewCustom time;
        @BindView(R.id.ic_dot)
        ImageView dot;
        @BindView(R.id.layout_item)
        LinearLayout layoutItem;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    public interface ProfileCallBack {
        public void onSelectProfile(Profile profile);

        public void optionProfile(Profile profile);

        public void optionProfileFromMain(Profile profile);

        public void onGuest();
    }
}
